﻿
namespace Leetcode.N0388;

public class Solution
{
    public int LengthLongestPath(string input)
    {
        int maxLength = 0;
        var dirs = new List<int>();
        int dirDeep = 0;
        dirs.Add(0);
        int fromIndex = 0;
        bool isFile = false;

        for (int i = 0; i < input.Length; i++)
        {
            var c = input[i];
            switch (c)
            {
                case '\n':
                    if (isFile)
                    {
                        maxLength = Math.Max(maxLength, i - fromIndex + dirs[dirDeep]);
                    }
                    else
                    {
                        dirDeep++;
                        if (dirDeep == dirs.Count)
                        {
                            dirs.Add(0);
                        }
                        dirs[dirDeep] = i - fromIndex + 1 + dirs[dirDeep - 1];
                    }
                    fromIndex = i + 1;
                    dirDeep = 0;
                    isFile = false;
                    break;
                case '\t':
                    dirDeep++;
                    fromIndex = i + 1;
                    break;
                case '.':
                    isFile = true;
                    break;
            }
        }

        if (isFile)
        {
            maxLength = Math.Max(maxLength, input.Length - fromIndex + dirs[dirDeep]);
        }
        return maxLength;
    }
}
